<?php
/**
 * Created by PhpStorm.
 * User: Sky
 * Date: 2017/8/1
 * Time: 下午4:17
 */

namespace app\controllers;

use app\librarys\ApiResponseCode;
use app\librarys\BaseApiController;
use app\librarys\MobilePhoneAreaQuery;
use app\librarys\RegeoSdk;
use app\librarys\Tools;
use app\models\BalancePay;
use app\models\Group;
use app\models\Merchant;
use app\models\Ad;
use app\models\MerchantGroup;
use app\models\User;
use app\models\WxPay;

/**
 * 商户端Api接口。需要商户登录后使用
 * @package app\controllers
 */
class ApiMerchantController extends BaseApiController
{
    private $currentMerchant = null;

    public function beforeAction($action)
    {
        $this->currentMerchant = Merchant::getCurrentMerchant();
        if ($this->currentMerchant == null) {
            echo $this->renderJson(null, ApiResponseCode::MerchantSessionErrorCode, ApiResponseCode::MerchantSessionErrorMessage);
            return false;
        }

        if (self::IsFormRequest == true) {
            $this->_request = \Yii::$app->request;
        } else {
            $this->_request = json_decode(file_get_contents("php://input"));
        }

        $token = $this->_getRequestValue('merchant_token');
        $merchantId = Merchant::authMerchantToken($token);
//        $merchantId = 'a81b93cc-b61d-11e7-84d9-00163e06b8e1';
        $merchant = Merchant::getById($merchantId);
        if ($merchant == null) {
            echo $this->renderJson(null, ApiResponseCode::UserSessionErrorCode, ApiResponseCode::UserSessionErrorMessage);
            return false;
        } else {
            Merchant::setCurrentMerchant($merchant);
        }

        return parent::beforeAction($action);
    }

    /**
     * 获取广告行业数据
     * @return string
     */
    public function actionGetAdIndustryType()
    {
        //$id = $this->currentMerchant['id'];
        //debug
        //$id = '549f1e00-7676-11e7-8eda-00163e06b8e1';

        $item = Merchant::getAdIndustryTypeById();
        return $this->renderJson($item, is_null($item) ? 1 : 0, is_null($item) ? 'ad type does not exist!' : 'success');
    }

    /**
     * 获取商户详细信息
     */
    public function actionGetMerchantAuth()
    {
        $id = Merchant::getCurrentMerchant()['id'];


        $item = Merchant::getAuthById($id);
        return $this->renderJson($item, is_null($item) ? 1 : 0, is_null($item) ? '该商户未注册' : 'success');
    }

    /**
     * 认证商家数据提交
     */
    public function actionSubmitMerchantData()
    {

        //id
        $id = Merchant::getCurrentMerchant()['id'];

        // var_dump('wo lai kankan id:'.$id);exit;
        //
        $name = $this->_getRequestValue('name');
        $registration_no = $this->_getRequestValue('registration_no');
        $business_license_scanning_copy = $this->_getRequestValue('business_license_scanning_copy_url');
        $legal_representative_id_front_photo = $this->_getRequestValue('legal_representative_id_front_photo_url');
        $legal_representative_id_back_photo = $this->_getRequestValue('legal_representative_id_back_photo_url');
        $legal_representative_id_photo = $this->_getRequestValue('legal_representative_id_photo_url');
        $legal_representative_id_number = $this->_getRequestValue('legal_representative_id_number');


        //
        $tel = $this->_getRequestValue('tel');
        $province = $this->_getRequestValue('province');
        $city = $this->_getRequestValue('city');
        $county = $this->_getRequestValue('county');
        $address = $this->_getRequestValue('address');
        $lon = $this->_getRequestValue('lon');
        $lat = $this->_getRequestValue('lat');


        $result = Merchant::setMerchantData($id, $name, $registration_no, $business_license_scanning_copy, $legal_representative_id_front_photo, $legal_representative_id_back_photo, $legal_representative_id_photo, $legal_representative_id_number, $tel, $province, $city, $county, $address, $lon, $lat);

        if ($result == null) {
            return $this->renderJson(null, 1, '保存信息失败，请重新提交');
        }

        if ($result['auth_result']) {
            return $this->renderJson($result['id'], 0, 'success');
        }
        return $this->renderJson($result['id'], 2, '您填写的证件号码与提交图片不一致，或者图片清晰度不够，请核对后重新填写，或者等待人工审核!');
    }

    /**
     * 新增广告内容(提交审核)
     * @return string
     */
    public function actionAddAd()
    {

        $id = Merchant::getCurrentMerchant()['id'];

        $industry_type_id = $this->_getRequestValue('industry_type_id');
        $title = $this->_getRequestValue('title');
        $content = $this->_getRequestValue('content');
        $lon = $this->_getRequestValue('lon');
        $lat = $this->_getRequestValue('lat');
        $video = $this->_getRequestValue('video', '');
        $image_main = $this->_getRequestValue('image_main');
        $image_urls = $this->_getRequestValue('image_urls', '');
        //var_dump($image_urls);

        $id = Merchant::saveAd($id, $industry_type_id, $title, $content, $lon, $lat, $video, $image_main, $image_urls);
        $data = [];
        if ($id) {
            $data['ad_id'] = $id;
            return $this->renderJson($data);
        }
        return $this->renderJson(null, 1, '新增广告失败');
    }

    /**
     * 获取当前商户已发布、已开始、已结束广告
     */
    public function actionGetReleasedAdByMerchant()
    {
        $merchant_id = Merchant::getCurrentMerchant()['id'];


        $page = $this->_getRequestValue('page', 0);
        $pageSize = $this->_getRequestValue('page_size', 20);

        $item = Merchant::getAdByMerchantId($page, $pageSize, $merchant_id);

        return $this->renderJson($item, is_null($item) ? 1 : 0, is_null($item) ? '当前用户无广告' : 'success');
    }

    /**
     * 获取当前商户信息
     * @return string
     */
    public function actionGetMerchant()
    {
        $merchant_id = Merchant::getCurrentMerchant()['id'];

        $item = Merchant::getById($merchant_id);
        return $this->renderJson($item, is_null($item) ? 1 : 0, is_null($item) ? 'id error' : 'success');
    }

    /**
     * update 商户信息
     * @return string
     */
    public function actionUpdateMerchant()
    {
        $merchant_id = Merchant::getCurrentMerchant()['id'];
//$id, $name,$tel,$address,$gravatar
        $name = $this->_getRequestValue('name');
        $tel = $this->_getRequestValue('tel');
        $address = $this->_getRequestValue('address');
        $gravatar = $this->_getRequestValue('gravatar');
        $url = $this->_getRequestValue('url', '');
        //$ = $this->_getRequestValue('');
        $item = Merchant::setMerchant($merchant_id, $tel, $address, $gravatar, $name, $url);
//       var_dump($item);
        return $this->renderJson($merchant_id);
    }

    /**
     * 商户增加意见
     */
    public function actionAddFeedback()
    {
        $origin_id = Merchant::getCurrentMerchant()['id'];

        $content = $this->_getRequestValue('content');
        //$origin_id = $this->_getRequestValue('origin_id');
        $origin = 1;

        Merchant::addFeedback($content, $origin, $origin_id);
        //return $origin_id;
        return $this->renderJson($origin_id);
    }

    /**
     * 查询商户余额
     */
    public function actionQueryAmount()
    {
        $id = Merchant::getCurrentMerchant()['id'];

        $item = Merchant::queryAmount($id);
        return $this->renderJson($item);
    }

    /**
     * 查询当前商户已经加入平台
     */
    public function actionQueryGroups()
    {
        $id = Merchant::getCurrentMerchant()['id'];

        $item = Merchant::getGroupsByMerchantId($id);
        return $this->renderJson($item, is_null($item) ? 1 : 0, is_null($item) ? '没有加入平台哟!' : 'success');

    }


    /**
     * 加入平台
     */
    public function actionAddGroup()
    {
        $id = Merchant::getCurrentMerchant()['id'];

        $keyword = $this->_getRequestValue('keyword');
        $name = $this->_getRequestValue('name');
        $tel = $this->_getRequestValue('tel');
        //$content = $this->_getRequestValue('content');

        $item = Merchant::applyGroup($id, $keyword, $name, $tel);

        return $this->renderJson($item, is_null($item) ? 1 : 0, is_null($item) ? '没有匹配的平台哟!' : 'success');

    }

    public function actionQuitGropup()
    {
        $id = Merchant::getCurrentMerchant()['id'];

        $group_id = $this->_getRequestValue('group_id');

        $item = Merchant::quitAGroup($id, $group_id);

        return $this->renderJson($item, is_null($item) ? 1 : 0, is_null($item) ? '没有匹配的平台哟!' : 'success');
    }


    /**
     * 添加商户支付记录
     */
    public function actionMerchantPayRecord()
    {


    }

    /**
     * 按商户获取所有广告列表
     */
    public function actionGetAdList()
    {
        $id = Merchant::getCurrentMerchant()['id'];
        $page = $this->_getRequestValue('page', 0);
        $pageSize = $this->_getRequestValue('page_size', 20);


        $item = Ad::getAdListByMerchantID($id, $page, $pageSize);
//Merchant::setGiftPaySuccess('26d594e6-8b03-11e7-84d9-00163e06b8e1');
        return $this->renderJson($item, is_null($item) ? 1 : 0, is_null($item) ? '当前商家无广告' : 'success');
    }

    /**
     * 设置广告红包信息
     */
    public function actionSetAdCashGift()
    {
        $ad_id = $this->_getRequestValue('ad_id');
        $one_cash = $this->_getRequestValue('one_cash');
        $publish_type = $this->_getRequestValue('publish_type');
        $time_of_ad = $this->_getRequestValue('time_of_ad');//单位为天
        $item = Ad::setAdCashGift($ad_id, $one_cash, $publish_type, $time_of_ad);

        return $this->renderJson($item, is_null($item) ? 1 : 0, is_null($item) ? 'ad_id无效' : 'success');
    }

//    public function actionGetGroups()
//    {
//        $merchant_id = Merchant::getCurrentMerchant()['id'];
//
//        $item = Merchant::getGroupsByMerchantId($merchant_id);
//        return $this->renderJson($item, is_null($item) ? 1 : 0, is_null($item) ? 'ad_id无效' : 'success');
//    }

    /**
     * 获取范围内、用户类型、关键字筛选用户列表
     * @return string
     */
    public function actionGetUserListByDistance()
    {
        $merchant_id = Merchant::getCurrentMerchant()['id'];

        $search_range = $this->_getRequestValue('search_range');
        $lon = $this->_getRequestValue('lon');
        $lat = $this->_getRequestValue('lat');
        $page = $this->_getRequestValue('page', 0);
        $pageSize = $this->_getRequestValue('page_size', 20);

        $tag = $this->_getRequestValue('tag', '');
        $serach_word = $this->_getRequestValue('search_word', '');
        $hobby = $this->_getRequestValue('hobby', '');
        $group_id = $this->_getRequestValue('group_id', '');
        $school = $this->_getRequestValue('school', '');
        $occupation = $this->_getRequestValue('occupation', '');
        $company = $this->_getRequestValue('company', '');
        $age_start = $this->_getRequestValue('age_start', 0);
        $age_end = $this->_getRequestValue('age_end', 100);
        $gender = $this->_getRequestValue('gender', -1);
        $isTourist = $this->_getRequestValue('isTourist', 0);


        $item = Merchant::getUserList($page, $pageSize, $lon, $lat, $search_range, $tag, $occupation, $company, $serach_word, $merchant_id, $hobby, $group_id, $school, $age_start, $age_end, $gender, $isTourist);
        return $this->renderJson($item, is_null($item) ? 1 : 0, is_null($item) ? 'ad_id无效' : 'success');
    }


    /**
     * 设置广告定向人群
     */
    public function actionSubmitAdUsers()
    {
        $ad_id = $this->_getRequestValue('ad_id');
        $userIds = $this->_getRequestValue('userIds');
        $item = null;
        if ($userIds != null)
            $item = Merchant::submitUsers($ad_id, $userIds);
        return $this->renderJson($item, is_null($item) ? 1 : 0, is_null($item) ? '添加失败' : 'success');
    }

    /**
     * 设置红包口令
     */
    public function actionSetGiftWord()
    {
        $ad_id = $this->_getRequestValue('ad_id');
//        $word = $this->_getRequestValue('word');
        $question = $this->_getRequestValue('question');
        $answer_a = $this->_getRequestValue('answer_right');
        $answer_b = $this->_getRequestValue('answer_error');

        $item = Merchant::setGiftWord($ad_id, $question, $answer_a, $answer_b);
        return $this->renderJson($item, $item < 0 ? 1 : 0, $item < 0 ? '设置失败' : 'success');
    }

    /**
     * 发布定点广告
     */
    public function actionReleaseAdByArea()
    {
        $ad_id = $this->_getRequestValue('ad_id');
        $search_range = $this->_getRequestValue('search_range');
        $lon = $this->_getRequestValue('lon');
        $lat = $this->_getRequestValue('lat');
        $item = Merchant::releaseAdByArea($ad_id, $lon, $lat, $search_range);
        return $this->renderJson($item, $item < 0 ? 1 : 0, $item < 0 ? '设置失败' : 'success');

    }

    /**
     * 设置定点广告的红包数和总金额
     * @return string
     */
    public function actionSetAreaAdGiftCount()
    {
        $ad_id = $this->_getRequestValue('ad_id');
        $count = $this->_getRequestValue('count');
        $total = $this->_getRequestValue('total');
        $item = Merchant::setAreaAdGiftCount($ad_id, $count, $total);
        $fail_message = '';
        if ($item == -1) {
            $fail_message = '广告不存在';
        }
        return $this->renderJson($item, $item < 0 ? 1 : 0, $item < 0 ? $fail_message : 'success');

    }


    /**
     * 返回商家认证状态
     */
    public function actionGetMerchantAuthStatus()
    {
        $merchant_id = Merchant::getCurrentMerchant()['id'];
        $item = Merchant::getMerchantAuthStatus($merchant_id);
        return $this->renderJson($item, $item < 0 ? 1 : 0, $item < 0 ? '商户未认证或者未启用' : 'success');

    }

    /**
     * 支付并发布广告
     */
    public function actionPayAd()
    {
        $ad_id = $this->_getRequestValue('ad_id');
        $total = $this->_getRequestValue('total');
        $pay_type = $this->_getRequestValue('pay_type');
    }

    /**
     * 获取商家粉丝
     * @return string
     */
    public function actionGetMyFuns()
    {
        $merchant_id = Merchant::getCurrentMerchant()['id'];
        $page = $this->_getRequestValue('page', 0);
        $pageSize = $this->_getRequestValue('page_size', 20);
        $item = Merchant::getMerchantFuns($merchant_id, $page, $pageSize);
        return $this->renderJson($item, is_null($item) ? 1 : 0, is_null($item) ? 'no funs!' : 'success');

    }

    /**
     * 获取广告报表
     */
    public function actionGetAdReport()
    {
//        $ad_id = $this->_getRequestValue('ad_id');
        $merchant_id = Merchant::getCurrentMerchant()['id'];
        $item = null;
        $item['report'] = Merchant::getAdReadTimes($merchant_id);
        $item['ad'] = Merchant::getAdReport($merchant_id);

        return $this->renderJson($item, is_null($item) ? 1 : 0, is_null($item) ? '没有数据哟!' : 'success');
    }

    /**
     * 获取系统消息
     */
    public function actionGetMessages()
    {
//        $merchant_id = Merchant::getCurrentMerchant()['id'];

        $page = $this->_getRequestValue('page', 0);
        $pageSize = $this->_getRequestValue('page_size', 20);
        $item = Merchant::getMessages(1, $page, $pageSize);

        return $this->renderJson($item, is_null($item) ? 1 : 0, is_null($item) ? '没有消息拉!' : 'success');

    }

    /**
     * 获取营销宝典列表
     * @return string
     */
    public function actionGetSaleArticleList()
    {
        $page = $this->_getRequestValue('page', 0);
        $pageSize = $this->_getRequestValue('page_size', 20);
        $item = Merchant::getSaleArticleList($page, $pageSize);
        return $this->renderJson($item, is_null($item) ? 1 : 0, is_null($item) ? '没有宝典哟!' : 'success');

    }

    /**
     * 获取营销宝典
     * @return string
     */
    public function actionGetSaleArticle()
    {
        $id = $this->_getRequestValue('id');
        $item = Merchant::getSaleArticle($id);
        return $this->renderJson($item, is_null($item) ? 1 : 0, is_null($item) ? '没有宝典哟!' : 'success');

    }


    /**
     * 广告红包支付
     * @return string
     */
    public function actionPayAdCashGift()
    {
        $ad_id = $this->_getRequestValue('ad_id'); //广告id;
        $data = WxPay::payAdCashGift($ad_id);

        file_put_contents('../web/logs/' . date('YmdHis') . 'PayAdCashGift.txt', $data);

        if ($data['result_code'] == 'SUCCESS' && $data['return_code'] == 'SUCCESS') {

            $params = [
                'appid' => $data['appid'],
                'partnerid' => $data['mch_id'],
                'prepayid' => $data['prepay_id'],
                'package' => 'Sign=WXPay',
                'noncestr' => Tools::getRandomString(12),
                'timestamp' => time()
            ];

            $params['sign'] = WxPay::makeSign($params);
            return $this->renderJson($params);
        }
        return $this->renderJson($data, 1, '生成支付失败');

    }

    /**
     * 特殊行业广告认证信息支付(默认300元)
     * @return string
     */
    public function actionPayAdSpecial()
    {
        $ad_id = $this->_getRequestValue('ad_id'); //广告id;
        $data = WxPay::payAdSpecial($ad_id);
        if ($data['result_code'] == 'SUCCESS' && $data['return_code'] == 'SUCCESS') {

            $params = [
                'appid' => $data['appid'],
                'partnerid' => $data['mch_id'],
                'prepayid' => $data['prepay_id'],
                'package' => 'Sign=WXPay',
                'noncestr' => Tools::getRandomString(12),
                'timestamp' => time()
            ];

            $params['sign'] = WxPay::makeSign($params);
            return $this->renderJson($params);


        }
        return $this->renderJson($data, 1, '生成支付失败');
    }

    /**
     * 平台入驻支付
     * @return string
     */
    public function actionPayMerchantGroup()
    {
        $merchant_id = Merchant::getCurrentMerchant()['id'];
        $merchant_id = $this->_getRequestValue('merchant_id', $merchant_id); //入驻商户id
        $month_sum = $this->_getRequestValue('$month_sum', 12); //入驻月数，默认12个月
        $mount_amount = $this->_getRequestValue('mount_amount', 5); //租金，默认5元

        $data = WxPay::payMerchantGroup($merchant_id, $month_sum, $mount_amount);
        if ($data['result_code'] == 'SUCCESS' && $data['return_code'] == 'SUCCESS') {

            $params = [
                'appid' => $data['appid'],
                'partnerid' => $data['mch_id'],
                'prepayid' => $data['prepay_id'],
                'package' => 'Sign=WXPay',
                'noncestr' => Tools::getRandomString(12),
                'timestamp' => time()
            ];

            $params['sign'] = WxPay::makeSign($params);
            return $this->renderJson($params, 0, '生成支付成功');
        }
        return $this->renderJson($data, 1, '生成支付失败');
    }


    /**
     * 获取余额广告红包支付金额
     */
    public function actionGetBalancePayAdCashGiftAmount()
    {
        $ad_id = $this->_getRequestValue('ad_id'); //广告id;
        $message = '';

        $amount = BalancePay::getPayAdCashGiftAmount($ad_id, $message);
        $data = [];
        if ($amount) {
            $data['amount'] = $amount;
            return $this->renderJson($data);
        }
        return $this->renderJson(null, 1, '获取余额广告红包支付金额失败，原因是：' . $message);
    }

    /**
     * 余额广告红包支付
     */
    public function actionBalancePayAdCashGift()
    {
        $ad_id = $this->_getRequestValue('ad_id'); //广告id;
        $message = '';
        $result = BalancePay::payAdCashGift($ad_id, $message);
        if ($result) {
            Merchant::setGiftPaySuccess($ad_id);
            return $this->renderJson(null, 0, '支付成功');
        }
        return $this->renderJson($data = [
            'message' => $message
        ], 1, '支付失败');
    }

    /**
     * 获取特殊行业广告认证信息支付金额(300)
     * @return string
     */
    public function actionGetBalancePayAdSpecialAmount()
    {
//        $ad_id = $this->_getRequestValue('ad_id'); //广告id;
//        $message = '';

        $data = [
            'amount' => 300
        ];

        return $this->renderJson($data);
    }


    /**
     * 特殊行业广告认证信息支付(默认300元)
     */
    public function actionBalancePayAdSpecial()
    {
        $ad_id = $this->_getRequestValue('ad_id'); //广告id;
        $message = '';
        $result = BalancePay::payAdSpecial($ad_id, $message);
        if ($result) {
            return $this->renderJson(null, 0, '支付成功');
        }
        return $this->renderJson($data = [
            'message' => $message
        ], 1, '支付失败');
    }

    /**
     * 获取平台入驻支付金额
     * @return string
     */
    public function actionGetBalancePayMerchantGroupAmount()
    {
        $merchant_id = Merchant::getCurrentMerchant()['id'];
        $merchant_id = $this->_getRequestValue('merchant_id', $merchant_id); //入驻商户id
        $month_sum = $this->_getRequestValue('$month_sum', 12); //入驻月数，默认12个月
        $message = '';

        $amount = 12 * 5;
        if ($amount) {
            $data = [
                'amount' => $amount
            ];
            return $this->renderJson($data);
        }

        return $this->renderJson(null, 1, '获取平台入驻支付金额失败，原因是：' . $message);
    }

    /**
     * 平台入驻支付
     */
    public function actionBalancePayMerchantGroup()
    {
        $merchant_id = Merchant::getCurrentMerchant()['id'];
        $merchant_id = $this->_getRequestValue('merchant_id', $merchant_id); //入驻商户id
        $month_sum = $this->_getRequestValue('$month_sum', 12); //入驻月数，默认12个月
        $mount_amount = $this->_getRequestValue('mount_amount', 5); //租金，默认5元
        $message = '';
        $result = BalancePay::payMerchantGroup($merchant_id, $message, $month_sum, $mount_amount);
        if ($result) {
            $item = Merchant::getById($merchant_id);
            $data = [
                'rent_begin_datetime' => $item['rent_begin_datetime'],
                'rent_end_datetime' => $item['rent_end_datetime'],
                'rent_amount' => $item['rent_amount']
            ];
            return $this->renderJson($data, 0, '支付成功');
        }
        return $this->renderJson($data = [
            'message' => $message
        ], 1, '支付失败');
    }

    /**
     * 获取商户入驻能力信息
     * @return string
     */
    public function actionGetMerchantConfig()
    {
        $merchant_id = Merchant::getCurrentMerchant()['id'];
//        var_dump($merchant_id);
        $item = MerchantGroup::getMerchantConfig($merchant_id);

        if ($item == null) {
            $item['status'] = -1;
            $item['is_auth'] = 0;
            $item['name'] = 0;
            $item['search_range'] = 0;
            $item['content_length'] = 0;
            $item['image_count'] = 0;
            $item['video_size'] = 0;

        } else {
//           $item['is_auth']=1;
            $item['name'] = '5元月租';
            $item['search_range'] = 10000;
            $item['content_length'] = 100000;
            $item['image_count'] = 10000;
            $item['video_size'] = 100000;
            $item['status'] = 1;
        }
        return $this->renderJson($item, is_null($item) ? 1 : 0, is_null($item) ? 'fail!' : 'success');
    }

    /**
     * 设置商户pushid
     * @return string
     */
    public function actionSetPushId()
    {
        $merchant_id = Merchant::getCurrentMerchant()['id'];
        $push_id = $this->_getRequestValue('push_id');

        $item = Merchant::setPushId($merchant_id, $push_id);
        return $this->renderJson($item, is_null($item) ? 1 : 0, is_null($item) ? 'fail!' : 'success');
    }

    /**
     * 通过id获取ad
     * @return string
     */
    public function actionGetAd()
    {
        $ad_id = $this->_getRequestValue('ad_id');

        $item = User::getAd($ad_id);
        return $this->renderJson($item, is_null($item) ? 1 : 0, is_null($item) ? 'fail!' : 'success');
    }

    public function actionGetGroups()
    {
        $item = Group::getList(0, 1000);
        return $this->renderJson($item, is_null($item) ? 1 : 0, is_null($item) ? 'fail!' : 'success');
    }

    /**
     * 返回所有groups
     * @return string
     */
    public function actionGetAllGroups()
    {
        $item = Group::getAll();
        return $this->renderJson($item, is_null($item) ? 1 : 0, is_null($item) ? 'fail!' : 'success');

    }

    /**
     *重新发布广告
     */
    public function actionPublishAdAgain()
    {
        $merchant_id = Merchant::getCurrentMerchant()['id'];
        $ad_id = $this->_getRequestValue('ad_id');

        $item = Merchant::publishAdAgain($merchant_id, $ad_id);
//      var_dump($item);
        return $this->renderJson($item, is_null($item) ? 0 : 1, is_null($item) ? 'fail!' : 'success');

    }
}